/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hraplication;
import java.sql.*;

public class Konekcija
{
  Connection conn;

  public Konekcija()
  {
    try
    {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      String url = "jdbc:mysql://localhost/hrbaza";
      conn = DriverManager.getConnection(url, "fedja", "terzic");

    }
    catch (ClassNotFoundException ex) {System.err.println(ex.getMessage());}
    catch (IllegalAccessException ex) {System.err.println(ex.getMessage());}
    catch (InstantiationException ex) {System.err.println(ex.getMessage());}
    catch (SQLException ex)           {System.err.println(ex.getMessage());}
  }
  public void Prekini()
  {
      try
      {
        conn.close();
      }
      catch (SQLException ex)           {System.err.println(ex.getMessage());}
  }
  
  public Connection GetConnection(){
      return conn;
  }

  public String Insert(String komanda){
    try
    {
      Statement st = conn.createStatement();
      System.out.println(komanda);
      st.executeUpdate(komanda);
    }
    catch (SQLException ex)
    {
      return ex.getMessage();
    }
    return null;

  }

  public ResultSet Select(String komanda){
    System.out.println(komanda);
    ResultSet rs = null;
    try
    {
      Statement st = conn.createStatement();
      rs = st.executeQuery(komanda);

    }
    catch (SQLException ex)
    {
      System.err.println(ex.getMessage());
    }
    return rs;
  }

  public String Delete(String komanda){
    System.out.println(komanda);
    try
    {
      Statement st = conn.createStatement();
      st.executeUpdate(komanda);
    }
    catch (SQLException ex)
    {
      return ex.getMessage();
    }
    return null;
  }


  private void doTests()
  {

    
    //doInsertTest(); 
    //doUpdateTest();  doSelectTest();
    //doDeleteTest();  doSelectTest();
  }

  private void doSelectTest()
  {
    System.out.println("[OUTPUT FROM SELECT]");
    String query = "SELECT COF_NAME, PRICE FROM COFFEES";
    try
    {
      Statement st = conn.createStatement();
      ResultSet rs = st.executeQuery(query);
      while (rs.next())
      {
        String s = rs.getString("COF_NAME");
        float n = rs.getFloat("PRICE");
        System.out.println(s + "   " + n);
      }
    }
    catch (SQLException ex)
    {
      System.err.println(ex.getMessage());
    }
  }

  private void doInsertTest()
  {
    System.out.print("\n[Performing INSERT] ... ");
    try
    {
      Statement st = conn.createStatement();
      st.executeUpdate("INSERT INTO COFFEES " +
                       "VALUES ('BREAKFAST BLEND', 200, 7.99, 0, 0)");
    }
    catch (SQLException ex)
    {
      System.err.println(ex.getMessage());
    }
  }

  private void doUpdateTest()
  {
    System.out.print("\n[Performing UPDATE] ... ");
    try
    {
      Statement st = conn.createStatement();
      st.executeUpdate("UPDATE COFFEES SET PRICE=4.99 WHERE COF_NAME='BREAKFAST BLEND'");
    }
    catch (SQLException ex)
    {
      System.err.println(ex.getMessage());
    }
  }

  private void doDeleteTest()
  {
    System.out.print("\n[Performing DELETE] ... ");
    try
    {
      Statement st = conn.createStatement();
      st.executeUpdate("DELETE FROM COFFEES WHERE COF_NAME='BREAKFAST BLEND'");
    }
    catch (SQLException ex)
    {
      System.err.println(ex.getMessage());
    }
  }
}


